In [1]:
#! pip install --user opencv-python
#! pip install --user matplotlib
# !pip install --user seaborn
# some dependencies might need to be installed
In [56]:
import cv2
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
import scipy as sp
from scipy import signal
import time as t
import skimage
from sklearn.preprocessing import normalize
In [3]:
IMAGE_PATH = './208.jpg'
image = cv2.imread(IMAGE_PATH)
In [4]:
def print_image(image):
cv_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(cv_rgb)
plt.show()
In [5]:
print_image(image)
In [6]:
channels = cv2.split(image)
colors = ('r', 'g', 'b')
In [7]:
histogram = [0.0]
for (channel, color) in zip(channels, colors):
histogram += cv2.calcHist([channel], [0], None, [256], [0, 256])
normalized_histogram = normalize(histogram, norm='l1', axis=0, copy=True, return_norm=False)
Then the width of 98% mass is calculated
In [8]:
def middleMassWidth(percentage, histogram):
bias = (int)((1 - percentage)/2)
accumulator = 0.0
start = 0
for index, bin_value in enumerate(histogram):
accumulator = accumulator + bin_value
if(accumulator < bias):
start = index
if(accumulator > bias + percentage):
return index - start - 1
In [9]:
middleMassWidth(0.98, normalized_histogram)
Out[9]:
In [10]:
middleMassWidth(0.95, normalized_histogram)
Out[10]:
In [11]:
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_image)
In [12]:
print(v)
The value channel ranges from 0 to 255. So, to get the brightness average percentage of the image we can use.
In [13]:
np.mean(v) / 256 * 100
Out[13]:
In [14]:
hue_countable = []
for rows in hsv_image:
for cols in rows:
if cols[2]/256 > 0.15 and cols[2]/256 < 0.95 and cols[1]/256 > 0.2:
hue_countable.append(cols[0])
In [15]:
print(len(hue_countable))
In [16]:
hue_count_histogram = np.histogram(hue_countable, bins=20)
In [17]:
print(hue_count_histogram)
In [18]:
m = np.max(hue_count_histogram[0])
alpha = 0.05
In [19]:
N = 0
for bin in hue_count_histogram[0]:
if bin > alpha * m:
N = N + 1
print(N)
In [20]:
qh = 20 - N
print(qh)
In [21]:
laplacian = np.array([ [0.2/1.2, 0.8/1.2, 0.2/1.2],
[0.8/1.2, -4/1.2, 0.8/1.2],
[0.2/1.2, 0.8/1.2, 0.2/1.2]])
print(laplacian)
In [22]:
r, g, b = cv2.split(image)
print(r)
In [37]:
r_laplacian = np.absolute(signal.convolve2d(r, laplacian, mode='same', boundary='fill', fillvalue=0))
g_laplacian = np.absolute(signal.convolve2d(g, laplacian, mode='same', boundary='fill', fillvalue=0))
b_laplacian = np.absolute(signal.convolve2d(b, laplacian, mode='same', boundary='fill', fillvalue=0))
mean_laplacian = np.mean([r_laplacian, g_laplacian, b_laplacian], axis=0)
mean_laplacian = mean_laplacian / mean_laplacian.max() * 255
print(np.max(mean_laplacian))
In [38]:
plt.imshow(mean_laplacian, cmap='gray_r')
plt.show()
In [58]:
resized_laplacian_image = sp.misc.imresize(mean_laplacian, [100, 100], interp='lanczos')
resized_laplacian_image = skimage.img_as_float(resized_laplacian_image)
normalize(resized_laplacian_image, norm='l1', axis=1, copy=False, return_norm=False)
resized_laplacian_image = resized_laplacian_image/100
In [59]:
plt.imshow(resized_laplacian_image, cmap='gray_r')
plt.show()
In [60]:
Px = np.sum(mean_normalized_laplacian, axis=0)
Py = np.sum(mean_normalized_laplacian, axis=1)
In [61]:
print(Px)
In [62]:
quality = 1 - middleMassWidth(0.98, Px) * middleMassWidth(0.98, Py)/10000
print("quality: ", quality)
In [110]:
fft_r = np.fft.fft2(r)
fft_g = np.fft.fft2(g)
fft_b = np.fft.fft2(b)
fft = np.mean([fft_r, fft_g, fft_b], axis=0)
In [111]:
fshift = np.fft.fftshift(fft)
magnitude_spectrum = 20*np.log(np.abs(fshift))
plt.plot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
In [112]:
np.abs(magnitude_spectrum).max()
(np.sum(np.abs(fft) > 5, axis=None)) / (len(fft) * len(fft[0]))
Out[112]:
In [ ]: